package com.example.nacos.nacosdemo.sockeserv;



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: homeWork
 * @description:
 * @author: hezhiqin
 * @create: 2020-03-29 22:17
 */
public class Soservice {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8080);//建立一个服务端，绑定一个端口
            Socket socket = serverSocket.accept();//接收客户端的连接（阻塞）
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));//拿到输入流

            //拿到输入流（阻塞， read/write阻塞）
            BufferedReader in=new BufferedReader(new InputStreamReader
                    (socket.getInputStream()));

            //输出流
            PrintWriter printWriter=new PrintWriter(socket.getOutputStream());
            //通过控制台拿到数据
            BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Client:"+in.readLine()); //获得输入流的信息
            String line=sin.readLine(); //获得控制台输入的数据
            while(!line.equals("bye")){
                printWriter.println(line); //写回到客户端
                printWriter.flush();

                System.out.println("client:"+in.readLine()); //读取客户端传过来的数据
                line=sin.readLine(); //重新读取控制台的数据
            }

            System.out.println(in.readLine()); //获得客户端的输入信息

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
